package com.guo.hui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guo.hui.dao.blog.TpCommentMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.blog.TpComment;
import com.guo.hui.service.CommentService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	private CommentService commentService;
	@Autowired
	private TpCommentMapper commentDao;

	@RequestMapping("/index")
	public String login() {
		return "blog/comment/index";
	}

	@ResponseBody
	@RequestMapping("/savedata")
	public String savedata(HttpServletRequest request, @RequestParam Integer aid, @RequestParam String comment) {
		System.out.println(request.getParameter("aid"));
		System.out.println(request.getParameter("comment"));
		TpComment tp = new TpComment();
		tp.setAid(aid);
		tp.setContent(comment);
		commentService.saveComment(tp);
		return "1";
	}
	

	@ResponseBody
	@RequestMapping("/commentdata")
	public String commentdata() {
		Map<String, Object> map = new HashMap<>();
		List<TpComment> list = commentService.selectByMap(map);
		return UtilJson.toJson(list);
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam int id) {
		TpComment tp = commentDao.selectById(id);
		tp.setDelflag(DelFlag.LOSE.toString());
		commentService.updateComment(tp);
		return "ok";
	}

}
